package p1gu;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

/**
 * Actual game.
 *
 * @author www.gametutorial.net
 */
public class Game {

    private int derrnierRespawn;
    private Vaisseau vaisseau;
    private ArrayList<MechantRoi> listDesMechantRoi;
    private ArrayList<Meteorite> listDesMeteorite;
    private ArrayList<Missile> listDesMissile;
    private boolean tirer;

    public Game() {
        Framework.gameState = Framework.GameState.GAME_CONTENT_LOADING;

        Thread threadForInitGame = new Thread() {
            @Override
            public void run() {
                // Sets variables and objects for the game.
                Initialize();
                // Load game files (images, sounds, ...)
                LoadContent();

                Framework.gameState = Framework.GameState.PLAYING;
            }
        };
        threadForInitGame.start();
    }

    /**
     * Set variables and objects for the game.
     */
    private void Initialize() {
        listDesMissile = new ArrayList<>();
        listDesMeteorite = new ArrayList<>();
        listDesMeteorite.add(new Meteorite(300, -70));
        listDesMechantRoi = new ArrayList<>();
        listDesMechantRoi.add(new MechantRoi(200, 50));
        listDesMechantRoi.add(new MechantRoi(250, -200));
        listDesMechantRoi.add(new MechantRoi(500, -100));
        listDesMechantRoi.add(new MechantRoi(800, -150));
        vaisseau = new Vaisseau(500, 600);

    }

    /**
     * Load game files - images, sounds, ...
     */
    private void LoadContent() {
    }

    /**
     * Restart game - reset some variables.
     */
    public void RestartGame() {
    }

    /**
     * Update game logic.
     *
     * @param gameTime gameTime of the game.
     * @param mousePosition current mouse position.
     */
    public void UpdateGame(long gameTime, Point mousePosition) {

        if (derrnierRespawn > 600) {
            
            int x = (int) (Math.random() * 892);
            listDesMechantRoi.add(new MechantRoi(x, -130));
            //adsf a
            x = (int) (Math.random() * 892);
            listDesMeteorite.add(new Meteorite(x, -130));
            derrnierRespawn=0;
        }else{
        derrnierRespawn+=gameTime;
        }

        Rectangle recMechantRoi, recMissil;

        if (tirer) {
            listDesMissile.add(new Missile(mousePosition.x - 67, mousePosition.y - 35));
            listDesMissile.add(new Missile(mousePosition.x + 33, mousePosition.y - 35));
            tirer = false;
        }
        for (Missile missile : listDesMissile) {
            missile.missile.y -= 0.5f * gameTime;
        }
        vaisseau.vaisseau.x = mousePosition.x - 64;
        vaisseau.vaisseau.y = mousePosition.y - 64;
        System.out.println(gameTime);

        for (MechantRoi mechantRoi : listDesMechantRoi) {
            mechantRoi.mechantRoi.y += 0.1f * gameTime;
        }
        for (Meteorite meteorite : listDesMeteorite) {
            meteorite.meteorite.y += 0.08f * gameTime;
        }

        for (Missile missile : listDesMissile) {

            recMissil = new Rectangle((int) missile.missile.x, (int) missile.missile.y, 32, 32);
            for (MechantRoi mechantRoi : listDesMechantRoi) {

                recMechantRoi = new Rectangle((int) mechantRoi.mechantRoi.x, (int) mechantRoi.mechantRoi.y, 128, 128);
                if (recMechantRoi.contains(recMissil)) {
                    listDesMechantRoi.remove(mechantRoi);
                    break;
                }
            }

        }

    }

    /**
     * Draw the game to the screen.
     *
     * @param g2d Graphics2D
     * @param mousePosition current mouse position.
     */
    public void Draw(Graphics2D g2d, Point mousePosition) {
        for (Missile missile : listDesMissile) {
            missile.draw(g2d);
        }
        for (MechantRoi mechantRoi : listDesMechantRoi) {
            mechantRoi.draw(g2d);
        }
        for (Meteorite meteorite : listDesMeteorite) {
            meteorite.draw(g2d);
        }
        vaisseau.draw(g2d);
    }

    public void mouseClicked(MouseEvent e) {
        tirer = true;
    }
}
